package com.my;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by YJH on 2017/7/12 22:52.
 */
public class UserDao {

    private static Map<String, User> userMap;
    private static List<Authority> authorityList = null;

    static {
        authorityList = new ArrayList<>();
        authorityList.add(new Authority("Article-1", "/article-1.jsp"));
        authorityList.add(new Authority("Article-2", "/article-2.jsp"));
        authorityList.add(new Authority("Article-3", "/article-3.jsp"));
        authorityList.add(new Authority("Article-4", "/article-4.jsp"));


        userMap = new HashMap<>();

        User user1 = new User("AAA", authorityList.subList(0, 2));   //subList(0,2)方法从0到2
        userMap.put("AAA", user1);

        user1 = new User("BBB", authorityList.subList(2, 4));
        userMap.put("BBB", user1);
    }

    User get(String username) {
        return userMap.get(username);
    }

    void update(String username, List<Authority> authorityList) {
        userMap.get(username).setAuthorityList(authorityList);
    }

    public List<Authority> getAuthorityList() {
        return authorityList;
    }

    public List<Authority> getAuthorityList(String[] urls) {
        List<Authority> authorities = new ArrayList<>();

        for (Authority authority : authorityList) {
            if (urls != null) {
                for (String url : urls) {
                    if (url.equals(authority.getUrl())) {
                        authorities.add(authority);
                    }
                }
            }
        }
        return authorities;
    }
}
